import { _decorator, Collider2D, Component, Contact2DType, Node } from 'cc';
import { GameManager, GameState } from '../GameManager';
const { ccclass, property } = _decorator;

@ccclass('Bullet_0')
export class Bullet_0 extends Component {
  @property
  bullet_0_Speed: number = 300;

  collider: Collider2D = null;

  hited: boolean = false;

  start() {
    this.collider = this.node.getComponent(Collider2D);
    this.collider.on(Contact2DType.BEGIN_CONTACT, () => {
      // console.log("Bullet");
      this.collider.off(Contact2DType.BEGIN_CONTACT)
      if (this.node) {
        this.hited = true;
      }
    }, this);

  }

  update(deltaTime: number) {
    if (GameManager.getInstance().gameState == GameState.START) {

      const newPos = this.node.getPosition();

      newPos.y -= this.bullet_0_Speed * deltaTime
      this.node.setPosition(newPos);

      if (newPos.y <= -1000) {
        this.node.destroy();
      }
    }
    if (this.hited) {
      if (this.node) {
        this.node.destroy();
      }
    }
  }
}


